/**********************************************************************
 * FILE : Queuepool.java
 * CREATE DATE : 2011-12-6
 * DESCRIPTION :
 *		
 *      
 * CHANGE HISTORY LOG
 *---------------------------------------------------------------------
 * NO.|    DATE    |     NAME     |     REASON     | DESCRIPTION
 *---------------------------------------------------------------------
 * 1  | 2011-12-6 |  Sting  |    草创版本
 *---------------------------------------------------------------------              
 **********************************************************************
 */
package com.sgm.de.benchmark.act;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

import com.sgm.de.benchmark.ProcessContext;

/**
 * @author Sting
 * TODO 主要作用:
 *		注意事项:
 */
public class Queuepool  extends AbstractAct {

    /*
     * (non-Javadoc)
     * @see com.sgm.de.benchmark.act.AbstractAct#exec(com.sgm.de.benchmark.ProcessContext)
     */
    @Override
    public void exec(ProcessContext ctx) throws Exception {
        if (ctx.queuePool == null) {
            ctx.queuePool = new ArrayBlockingQueue<String[]>(5000);
        } else
            ctx.queuePool.clear();

        Queue<String[]> q = ctx.queuePool;

        String firstQueue = args[0].toString();

        if (firstQueue.indexOf("?") > -1) {// 第一个参数有问号(?),则为模式匹配
            if (args.length > 3 && args[3].toString().indexOf("?") > -1) {// 同步模式,一个线程两个queue
                int firstFrom = Integer.valueOf(args[1].toString());
                int firstTo = Integer.valueOf(args[2].toString());
                String secondQueue = args[3].toString();
                int secondFrom = Integer.valueOf(args[4].toString());
                int secondTo = Integer.valueOf(args[5].toString());
                for (int i = firstFrom, j = secondFrom; i <= firstTo; i++, j++) {
                    String[] threadQueue = new String[2];
                    threadQueue[0] = firstQueue.replace("?", String.valueOf(i)).trim();
                    threadQueue[1] = secondQueue.replace("?", String.valueOf(j)).trim();
                    q.add(threadQueue);
                }
            } else {// 异步模式,一个线程一个queue
                int firstFrom = Integer.valueOf(args[1].toString());
                int firstTo = Integer.valueOf(args[2].toString());
                for (int i = firstFrom; i <= firstTo; i++) {
                    q.add(new String[] { firstQueue.replace("?", String.valueOf(i)).trim() });
                }
            }

        } else if (args.length == 4 && args[1].toString().indexOf("?") > -1) {
         //     线程第一queue固定,第2queue模板
         //     eg:to-sgm,from-sgm-?,1,1000
            String secondQueue = args[1].toString();
                int secondFrom = Integer.valueOf(args[2].toString());
                int secondTo = Integer.valueOf(args[3].toString());
                for (int i = secondFrom; i <= secondTo; i++) {
                    String[] threadQueue = new String[2];
                    threadQueue[0] = firstQueue;
                    threadQueue[1] = secondQueue.replace("?", String.valueOf(i)).trim();
                    q.add(threadQueue);
                }
        } else {// 否则为枚举名称
            for (Object obj : args) {
                ctx.queuePool.add(new String[] { obj.toString().trim() });
            }
        }

    }
}
